import 'dotenv/config';
import type { VercelRequest, VercelResponse } from '@vercel/node'; 
import { createAgenda, deleteAgenda, getAgenda, updateAgenda } from '../../service/agenda/AgendaService';
import { Agenda, CreateAgendaCmd, UpdateAgenda } from '../../dto/agenda';
import { agendaProcess } from '../../dal/agenda/AgendaRepo';
import { successWithData } from '../../util/result';

export default async(request: VercelRequest, response: VercelResponse) => {
  const {method}=request;
  if (method === 'POST') {

    const{id,name,description,startTime,endTime,timeDetail,isImportant}=request.body as UpdateAgenda;
    const agenda:UpdateAgenda={id,name,description,startTime,endTime,timeDetail,isImportant};
    const data=await updateAgenda(agenda);
    return response.json(successWithData(data));

  }else if (method === 'GET') {
    
    console.log("dfdsfsd");
    const { timestamp } =request.query;
    const data =await getAgenda(Number(timestamp));
    //TODO
    const agenda:Agenda[]= agendaProcess(data);
    console.log("dfdsfsd");
    return response.json(successWithData(agenda));

  }else if(method === 'DELETE'){

    console.log("dfdsfsd");
    const { id }=request.query; 
    const data =await deleteAgenda(Number(id[0]));
    return response.json(successWithData(data));

  }else if(method === 'PUT'){

    const{name,description,startTime,endTime,timeDetail,isImportant}=request.body as CreateAgendaCmd;
    const agenda:CreateAgendaCmd={name,description,startTime,endTime,timeDetail,isImportant};
    const data=await createAgenda(agenda);
    return response.json(successWithData(data));

  }else {
    return response.status(405).json({ error: 'Method Not Allowed' });
  }
}
